package org.afun.csms_android;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.afun.csms_android.adapter.Teacher2CourseSelectAdapter;
import org.afun.csms_android.adapter.ViewPagerAdapter;
import org.afun.csms_android.domain.Student;
import org.afun.csms_android.domain.Teacher;
import org.afun.csms_android.domain.User;
import org.afun.csms_android.fragment.ManagerAudit;
import org.afun.csms_android.fragment.ManagerHome;
import org.afun.csms_android.fragment.ManagerMenu;
import org.afun.csms_android.fragment.Student2CourseOperator;
import org.afun.csms_android.fragment.StudentShowScore;
import org.afun.csms_android.fragment.Teacher2CourseOperator;
import org.afun.csms_android.fragment.TeacherHome;
import org.afun.csms_android.tools.Music;
import org.afun.csms_android.utils.MyDatabaseHelper;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

import java.io.Serializable;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    private ViewPager viewPager;
    private static TableOperator tableOperator;

    // Fragments
    MenuItem prevMenuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        MyTableOperator myTableOperator = new MyTableOperator(this);
        MainActivity.tableOperator = myTableOperator.getTableOperator();


//        MyDatabaseHelper myDatabaseHelper = new MyDatabaseHelper(this);
//        SQLiteDatabase sqLiteDatabase = myDatabaseHelper.getWritableDatabase();
//        // 更新数据库
//        myDatabaseHelper.onUpgrade(sqLiteDatabase, 1, 2);

        //Initializing viewPager
        viewPager = (ViewPager) findViewById(R.id.viewpager);

        //Initializing the bottomNavigationView
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.action_first:
                                viewPager.setCurrentItem(0);
                                break;
                            case R.id.action_second:
                                viewPager.setCurrentItem(1);
                                break;
                            case R.id.action_third:
                                viewPager.setCurrentItem(2);
                                break;
                        }
                        return false;
                    }
                });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (prevMenuItem != null) {
                    prevMenuItem.setChecked(false);
                } else {
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);
                }
                Log.d("page", "onPageSelected: " + position);
                bottomNavigationView.getMenu().getItem(position).setChecked(true);
                prevMenuItem = bottomNavigationView.getMenu().getItem(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        setupViewPager(viewPager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.switch_item:
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.quit_item:
                finish();
                break;
            default:
        }
        return true;
    }

    public Context context() {
        return this;
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        Intent intent = getIntent();
        User user = (User) intent.getSerializableExtra("user");
//        User user=new User();
//        user.setAccount("30001");

        switch (user.getRoleId()) {
            case 1:
                // roldId==1: manager
                ManagerHome managerHome = new ManagerHome(this);
                adapter.addFragment(managerHome);
                ManagerAudit managerAudit = new ManagerAudit(this);
                adapter.addFragment(managerAudit);

                // start play the music
                Intent intentService = new Intent(this, Music.class);
                intentService.putExtra("action", 1);
                startService(intentService);
                break;
            case 2:
                // roleId==2: teacher
                TeacherHome teacherHome = new TeacherHome(this, user);
                adapter.addFragment(teacherHome);
                Teacher teacher = tableOperator.findTeacherByAccount(user.getAccount());
                Teacher2CourseOperator teacher2CourseOperator = new Teacher2CourseOperator(this, teacher);
                adapter.addFragment(teacher2CourseOperator);
                break;

            case 3:
                String account = user.getAccount();
                Student student = tableOperator.findStudentByAccount(account);
                StudentShowScore studentShowScore = new StudentShowScore(this, student);
                adapter.addFragment(studentShowScore);
                Student2CourseOperator student2CourseOperator = new Student2CourseOperator(this, student);
                adapter.addFragment(student2CourseOperator);
                break;
        }

        // self-msg fragment
        ManagerMenu managerMenu = new ManagerMenu(this, user);
        adapter.addFragment(managerMenu);
        viewPager.setAdapter(adapter);
    }
}